<html>
<body>
<h1> Calculator </h1>
<p> (Built on  4/09/2012 by Edvin Zohrabyan) </p>
<p> Type an expression in the box below (e.g., 10.5+20*3/25). </p>

<form action="./calculator.php" method="GET">
<input type="text" name="Expr" />
<input type="Submit" VALUE="Calculate"/>
</form>

<?php  //12
if($_GET["Expr"]){
	$equ= $_GET["Expr"];

	// check for "/0" or "--"
	$invalid = "#^.*(\/0(\+|\-|\*|\/|$)|[-][-]).*#i";
	// checks if input is a valid expression
	$valid = "#[ ]*[-]?\d*[.]?\d+([ ]*([+]|[-]|[*]|[/]|)[ ]*[-]?\d*[.]?\d+)*[ ]*#i";

	preg_match($valid, $equ, $match2);
	
     // if input is valid call eval() functions
     if ((preg_match($invalid, $equ, $match1)==0)&&(strlen($match2[0]) == strlen($equ)))
	{
			eval("\$ans= $equ;");
			echo "Answer: ".$equ ." = ".$ans."<br/>";
	}
	else
	{
		echo "Invalid Input! Please enter a valid expression!"."<br/>";
		echo " - Only +,-,*,/ operators and numbers are allowed!"."<br/>";
		echo " - No division by zero is allowed!"."<br/>";
	}
} 

?>
</body>
</html> 
